package com.ssmTest.service.impl;

import com.ssmTest.dao.IMachineUserInformationDao;
import com.ssmTest.model.MachineUserInformation;
import com.ssmTest.model.Order;
import com.ssmTest.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.crypto.Mac;
import java.util.List;

@Service("machineuserInformationService")
public class MachineUserInformationServiceImpl implements IMachineUserInformationService {
    @Autowired
    IMachineUserInformationService iMachineUserInformationService;
    @Resource
    IMachineUserInformationDao iMachineUserInformationDao;
    @Autowired
    DateTimeService dateTimeService;
    @Autowired
    IUserInformationService iUserInformationService;
    @Autowired
    IOrderService iOrderService;
    @Autowired
    IUserService iUserService;

    //根据id查找订单
    public MachineUserInformation selectMachineUserInformation(int id) {
        return this.iMachineUserInformationDao.selectMachineUserInformation(id);
    }

    //插入订单
    public void insertMachineUserInformation(MachineUserInformation machineuserInformation) {
        this.iMachineUserInformationDao.insertMachineUserInformation(machineuserInformation);
    }

    //订单列表
    public List<MachineUserInformation> MachineInformationList(int te) {
        List<MachineUserInformation> list = iMachineUserInformationDao.machineUserInformationList(te);
        return list;
    }

    //审核失败后更新原因
    public void updateInformationReasion(MachineUserInformation machineUserInformation) {
        this.iMachineUserInformationDao.updateInformation(machineUserInformation);
    }

    //关键字查询
    public List<MachineUserInformation> selectUserInformationByAndName(int pagenum, String city, String name) {
        List<MachineUserInformation> list = iMachineUserInformationDao.selectUserInformationByAndName((pagenum - 1) * 10, pagenum * 10, city, name);
        return list;
    }

    public List<MachineUserInformation> selectUserInformationByNoName(int pagenum, String city) {
        List<MachineUserInformation> list = iMachineUserInformationDao.selectUserInformationByNoName((pagenum - 1) * 10, pagenum * 10, city);
        return list;
    }

    //订购信息
    public int orderMachineInformation(int id, int first_party, int second_party) {
        //-1该信息不存在 -2已被下单 -3订单状态异常 其它正数，订单id
        MachineUserInformation machineUserInformation = iMachineUserInformationService.selectMachineUserInformation(id);
        if (machineUserInformation == null)
            return -1;
        if (machineUserInformation.getToexamine() == 3)
            return -2;
        if (machineUserInformation.getToexamine() != 1)
            return -3;
        Order order = new Order();
        order.setFirst_party(first_party);
        order.setSecond_party(second_party);
        order.setTime(dateTimeService.getTime());
        order.setInformation(id);
        order.setToexamine(0);
        order.setType(2);
        order.setTitle(machineUserInformation.getTitle());


        machineUserInformation.setToexamine(3);
        iMachineUserInformationService.updateInformationReasion(machineUserInformation);

        return iOrderService.insertOrder(order);
    }

    public int numberOfInformation(int toexamine) {
        return this.iMachineUserInformationDao.numberOfInformation(toexamine);
    }

    public List<MachineUserInformation> selectByPersionalId(int personal_id) {
        return this.iMachineUserInformationDao.selectByPersionalId(personal_id);
    }
}
